<template>
	<view>
		<qiun-title-bar title="博客类别比例" />
		<view class="charts-box">
			<qiun-data-charts type="pie" :chartData="pie" :opts="opts" />
		</view>
		<qiun-title-bar title="最新博客分析" />
		<view class="charts-box">
			<qiun-data-charts type="column" canvasId="scrollcolumid"
				:opts="{enableScroll:true,xAxis:{scrollShow:true,itemCount:4,disableGrid:true}}" :ontouch="true"
				:canvas2d="true" :chartData="column" />
		</view>
		<qiun-title-bar title="常用标签词云" />
		<view class="charts-box">
			 <qiun-data-charts 
			      type="word"
			      :opts="opts"
			      :chartData="word"
			    />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pie: {},
				column: {},
				word: {},
				opts: {
					legend: {
						fontSize: 12
					}
				}
			}
		},
		onLoad() {
			this.getCategories() //获取类别数据
			this.getTopBlogs() //获取点赞数据和阅读量
			this.getTags() //获取标签
		},
		methods: {
			getTags() {
				this.word = {}
				let json = {
					"series": []
				}
				let word = JSON.parse(JSON.stringify(json))
				let url = this.$params.host + this.$params.action_tags
				console.log(url)
				this.$request.get(url, res => {
					url = this.$params.host + this.$params.action_tags_count
					res.data.forEach(async (t) => {
						let r = await this.$request.requestAsync(url + t.id)
						if (r.data.success && r.data.data > 0) {
							let tag = {
								name: t.name,
								textSize: parseInt(r.data.data) * 10>25?25:parseInt(r.data.data) *10
							}
							word.series.push(tag)
						}
					})
					console.log(word);
					this.word = word
				}, () => {
					uni.stopPullDownRefresh()
				})
			},

			getTopBlogs() {
				this.column = {}
				let json = {
					"categories": [],
					"series": [{
						"name": "阅读量",
						"data": []
					}, {
						"name": "评论量",
						"data": []
					}]
				}
				this.column = JSON.parse(JSON.stringify(json))
				let url = this.$params.host + this.$params.action_hot
				this.$request.get(url, res => {
					res.data.forEach(blog => {
						this.column.categories.push(this.$utli.ellipsis(blog.title, 10))
						this.column.series[0].data.push(blog.readCount)
						this.column.series[1].data.push(blog.comments.length)
					})
				}, () => {})

			},
			getCategories() {
				this.pie = {}
				let json = {
					"series": [{
						"data": []
					}]
				}
				this.pie = JSON.parse(JSON.stringify(json))
				let url = this.$params.host + this.$params.action_categories
				this.$request.get(url, res => {
					url = this.$params.host + this.$params.action_category_count
					res.data.forEach(async (c) => {
						let r = await this.$request.requestAsync(url + c.id)
						if (r.data.success && r.data.data > 0) {
							let category = {
								name: c.name,
								value: r.data.data
							}
							this.pie.series[0].data.push(category)
						}
					})
				}, () => {})
			}

		}
	}
</script>

<style>

</style>
